//
//  CourseCenterView.m
//  xpkc
//
//  Created by TopSageOSX on 14/12/19.
//  Copyright (c) 2014年 wanmeizty. All rights reserved.
//

#import "CourseCenterView.h"

@interface CourseCenterView (){
    UIScrollView *scroll;
    NSArray *array;
}

@end

@implementation CourseCenterView

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatView];
    }
    return self;
}

- (void)creatView{
    scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [self addSubview:scroll];
}

- (void)setCourseArray:(NSArray *)courseArray{
    array = [NSArray arrayWithArray:courseArray];
    [self updateView];
}
- (void)updateView{
    CGFloat totalHeight = 0;
    for (int j = 0; j<array.count; j++) {
        NSDictionary *dic = [array objectAtIndex:j];
        
        NSArray *deeparray = [dic objectForKey:@"deeparray"];
        
        UIView *course = [[UIView alloc]init];
        UILabel *title = [[UILabel alloc]init];
        UIImageView *imgview = [[UIImageView alloc]init];
        //位置
        //视图位置以及视图内部件位置
        CGFloat fontnum = SCREEN_WIDTH/18;
        CGFloat offwidth = 0.04*SCREEN_WIDTH;//间隔
        CGFloat iconwidth = 0.1*SCREEN_WIDTH;//图标大小
        CGFloat coursewidth = self.frame.size.width;
        CGFloat courseheight = coursewidth;
        CGFloat btnwidth = (coursewidth - 4*offwidth - iconwidth)/2;
        CGFloat btnheight = fontnum*1.5;
        
        
        CGRect coursetitle_label_frame,courseimg_img_frame;
        
        coursetitle_label_frame = CGRectMake(offwidth*2 + iconwidth, offwidth *2, 200, iconwidth);
        courseimg_img_frame = CGRectMake(offwidth, offwidth *2, iconwidth, iconwidth);
        //title
        title.text = [dic objectForKey:@"title"];
        title.font = FONT_BOLD(19);
        title.textColor = [UIColor blackColor];
        title.frame = coursetitle_label_frame;
        //img
        imgview.frame = courseimg_img_frame;
        UIImage *courseimg;
        UIColor *coursecolor;
        if ([[dic objectForKey:@"title"]isEqualToString:@"财会类"]) {
            courseimg = [UIImage imageNamed:@"Accounting"];
            coursecolor = UIColorFromRGB(0xffffff);
        }else if ([[dic objectForKey:@"title"]isEqualToString:@"外语类"]){
            courseimg = [UIImage imageNamed:@"English"];
            coursecolor = UIColorFromRGB(0xffffff);
        }else if ([[dic objectForKey:@"title"]isEqualToString:@"工程类"]){
            courseimg = [UIImage imageNamed:@"Engineering"];
            coursecolor = UIColorFromRGB(0xffffff);
        }else if ([[dic objectForKey:@"title"]isEqualToString:@"考研类"]){
            courseimg = [UIImage imageNamed:@"Accounting"];
            coursecolor = UIColorFromRGB(0xffffff);
        }
        imgview.image = courseimg;
        
        course.backgroundColor = coursecolor;
        int dicid = [[dic objectForKey:@"id"]intValue];
        
        for (int i = 0; i<deeparray.count; i++) {
            
            NSDictionary *deepdic = [deeparray objectAtIndex:i];
            logdebug(@"%@",[deepdic objectForKey:@"typename"]);
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setTitle:[deepdic objectForKey:@"typename"] forState:UIControlStateNormal];
            btn.tag = dicid *10000 + [[deepdic objectForKey:@"id"]intValue];
            [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            btn.titleLabel.font = FONT_SYS_NORMAL;
            btn.backgroundColor = WhiteSmokeColor;
            
            btn.layer.borderColor = [UIColor colorWithRed:234/255.0 green:235/255.0 blue:236/255.0 alpha:1.0].CGColor;
            btn.layer.borderWidth = 2;
            btn.layer.cornerRadius = 5;
            btn.layer.masksToBounds = YES;
            
            
            btn.frame = CGRectMake(offwidth+iconwidth + (offwidth*2+btnwidth)*(i%2), iconwidth + offwidth*2 + (offwidth/2+btnheight)*(i/2), btnwidth, btnheight);
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [course addSubview:btn];
            course.height = btn.bottom + offwidth*2;
        }
        course.left = 0;
        course.top = totalHeight;
        course.width = coursewidth;
        totalHeight += course.height + offwidth;
        [course addSubview:title];
        [course addSubview:imgview];
        [scroll addSubview:course];
        scroll.contentSize = CGSizeMake(SCREEN_WIDTH, course.bottom + offwidth);
    }
}


- (void)btnClick:(UIButton *)btn{
    logdebug(@"%ld",(long)btn.tag);
    [self.delegate courseCenterViewBtnClick:btn];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
